package cn.com.darly.vpm.bizapi;

import cn.com.darly.vpm.business.friend.bean.FriendShip;
import cn.com.darly.vpm.business.friend.service.FriendShipService;
import cn.com.darly.vpm.framework.base.BaseController;
import cn.com.darly.vpm.framework.cache.BaseCache;
import cn.com.darly.vpm.framework.cache.Cache;
import cn.com.darly.vpm.framework.common.DateUtil;
import cn.com.darly.vpm.framework.common.UuidGenerateUtil;
import cn.com.darly.vpm.mobile.api.MobileLoginApi;
import cn.com.darly.vpm.security.user.bean.Role;
import cn.com.darly.vpm.security.user.bean.SystemResource;
import cn.com.darly.vpm.security.user.bean.User;
import cn.com.darly.vpm.security.user.bean.UserRole;
import cn.com.darly.vpm.security.user.service.ResourceService;
import cn.com.darly.vpm.security.user.service.UserRoleService;
import cn.com.darly.vpm.security.user.service.UserService;
import cn.com.darly.vpm.system.type.bean.SystemDic;
import cn.com.darly.vpm.system.type.service.SystemDicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author : ZhangYuHui
 * Date : 2017/11/14
 * TODO :
 */
@Service
public class MobileLoginBiz extends BaseController implements MobileLoginApi {
    @Resource
    UserService userService;

    //直接使用Cache进行展示，获取数据库中的数据，进行展示。展示之前进行缓存刷新操作
    @Resource
    Cache cache;

    @Resource
    SystemDicService systemDicService;
    @Resource
    ResourceService resourceService;

    @Resource
    UserRoleService roleService;

    @Override
    public User login(String account) {
        return userService.findUserByLoginName(account);
    }

    @Override
    public boolean updateUser(User user) {
        int count =  userService.updateById(user);
        if (count>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean registerUser(User user) {
        //在用户手机端登录接口调用时，尝试刷新缓存。调用一次存入一条数据
        SystemDic dic = new SystemDic();
        dic.setId(UuidGenerateUtil.getUUIDLong());
        dic.setCode("abies");
        dic.setCreateby(user.getName());
        dic.setCreatetime(DateUtil.now());
        dic.setName("固执");
        dic.setGroupwith(1);
        dic.setUpdatetime(DateUtil.now());
        systemDicService.save(dic);

        SystemResource resource = new SystemResource();
        resource.setId(UuidGenerateUtil.getUUIDLong());
        resource.setType("leix");
        resource.setParentid("123");
        resource.setCode("abies");
        resource.setCreateBy(user.getName());
        resource.setCreateDate(DateUtil.now());
        resource.setName("固执");
        resource.setContent("aldfkjlasdkj");
        resource.setUpdateDate(DateUtil.now());
        resource.setUpdateBy(user.getName());
        resource.setDisabled(true);
        resourceService.save(resource);
        List<SystemResource> systemResources = (List<SystemResource>) cache.get(BaseCache.RESOURCE_CACHE);
        logger.info("systemResources>>"+systemResources.size());
        List<SystemDic> systemDics = (List<SystemDic>) cache.get(BaseCache.SYSTEM_CACHE);
        logger.info("systemDics>>"+systemDics.size());
        return userService.saveUser(user);
    }

    @Override
    public void setUserRole(String id,String dest) {
        UserRole role = new UserRole();
        role.setUserId(id);
       List<Role> roles =  roleService.findAll();
        for (Role rol:roles) {
            if (rol.getCode().toUpperCase().contains(dest.toUpperCase())){
                role.setRoleId(rol.getId());
                break;
            }
        }
        roleService.save(role);
    }
}
